#include "bsp_current.h"
#include "bsp_timer.h"

#define MAX_SETP 32
static uint32_t ticks_per_step_temp = 0;
static uint32_t ticks_per_step = 0;
static uint32_t ticks_check_step = 0;
static uint32_t hbridge_step =0;
static uint32_t current_coefficient = 0;


static const uint32_t hbridge_ticks_per_step[] = {
	0, 1250, 1406, 1562, 1718, 1875, 2031, 2187, 2343, 2500
//0, 40, 45, 50, 55, 60, 65, 70, 75, 80
};

static void _step_0()
{
	
	if(ticks_check_step == 0){
		/* H桥方向 */
		current_bridge_shutdown();
		/* 电流输出大小 */
		current_output(0);
	}	

}
static void _step_2() __attribute__((weakref, alias("_step_0")));
static void _step_4() __attribute__((weakref, alias("_step_0")));
static void _step_6() __attribute__((weakref, alias("_step_0")));
static void _step_8() __attribute__((weakref, alias("_step_0")));
static void _step_10() __attribute__((weakref, alias("_step_0")));
static void _step_12() __attribute__((weakref, alias("_step_0")));
static void _step_14() __attribute__((weakref, alias("_step_0")));
static void _step_16() __attribute__((weakref, alias("_step_0")));
static void _step_18() __attribute__((weakref, alias("_step_0")));
static void _step_20() __attribute__((weakref, alias("_step_0")));
static void _step_22() __attribute__((weakref, alias("_step_0")));
static void _step_24() __attribute__((weakref, alias("_step_0")));
static void _step_26() __attribute__((weakref, alias("_step_0")));
static void _step_28() __attribute__((weakref, alias("_step_0")));
static void _step_30() __attribute__((weakref, alias("_step_0")));

/***********
 * a->b
 */
static void _step_1()
{
	if(ticks_check_step == 0){
		/* H桥方向 */
		current_a_to_b();
		/* 电流输出大小 */
		current_output(current_coefficient*0.3826);
	}	
}
static void _step_27() __attribute__((weakref, alias("_step_1")));


static void _step_3()
{
	if(ticks_check_step == 0){
		/* H桥方向 */
		current_a_to_b();
		/* 电流输出大小 */
		current_output(current_coefficient*0.7071);
	}	
}
static void _step_25() __attribute__((weakref, alias("_step_3")));


static void _step_9()
{
	if(ticks_check_step == 0){
		/* H桥方向 */
		current_a_to_b();
		/* 电流输出大小 */
		current_output(current_coefficient*0.9238);
	}	
}
static void _step_19() __attribute__((weakref, alias("_step_9")));


static void _step_11()
{
	if(ticks_check_step == 0){
		/* H桥方向 */
		current_a_to_b();
		/* 电流输出大小 */
		current_output(current_coefficient);
	}	
}
static void _step_17() __attribute__((weakref, alias("_step_11")));

/***********
 * b->a
 */

static void _step_5()
{
	if(ticks_check_step == 0){
		/* H桥方向 */
		current_b_to_a();
		/* 电流输出大小 */
		current_output(current_coefficient*0.3826);
	}	
}
static void _step_31() __attribute__((weakref, alias("_step_5")));

static void _step_7()
{
	if(ticks_check_step == 0){
		/* H桥方向 */
		current_b_to_a();
		/* 电流输出大小 */
		current_output(current_coefficient*0.7071);
	}	
}
static void _step_29() __attribute__((weakref, alias("_step_7")));

static void _step_13()
{
	if(ticks_check_step == 0){
		/* H桥方向 */
		current_b_to_a();
		/* 电流输出大小 */
		current_output(current_coefficient*0.9238);
	}	
}
static void _step_23() __attribute__((weakref, alias("_step_13")));

static void _step_15()
{
	if(ticks_check_step == 0){
		/* H桥方向 */
		current_b_to_a();
		/* 电流输出大小 */
		current_output(current_coefficient);
	}	
}
static void _step_21() __attribute__((weakref, alias("_step_15")));


static const current_hbridge_step_tick_function step_funcs[MAX_SETP] = {
	_step_0,
	_step_1,
	_step_2,
	_step_3,
	_step_4,
	_step_5,
	_step_6,
	_step_7,
    _step_8,
	_step_9,
	_step_10,
	_step_11,
	_step_12,
	_step_13,
	_step_14,
	_step_15,

	_step_16,
	_step_17,
	_step_18,
	_step_19,
	_step_20,
	_step_21,
	_step_22,
	_step_23,
    _step_24,
	_step_25,
	_step_26,
	_step_27,
	_step_28,
	_step_29,
	_step_30,
	_step_31,

};

static void _tick_service(void)
{
	ticks_check_step++;	
	if(ticks_per_step_temp == 0){
		ticks_check_step = 0;
		return;
	}
	if(ticks_check_step >= ticks_per_step/TIMER2_BASE_TICK){
		ticks_check_step = 0;
		hbridge_step++;
		if(ticks_per_step != ticks_per_step_temp){
			ticks_per_step = ticks_per_step_temp;
		}	
		if(hbridge_step >= MAX_SETP){
			hbridge_step = 0;
		}
	}
	step_funcs[hbridge_step]();
}

static void _set_frequecy(uint32_t level)
{
	ticks_per_step_temp = hbridge_ticks_per_step[level];
}

static void _set_elec(uint32_t ma_level)
{
	current_coefficient = current_trans_intensity(ma_level);
}

static void _start_output(void)
{
	ticks_per_step_temp = ticks_per_step;
	ticks_check_step = 0;
	hbridge_step = 0;
	current_bridge_shutdown();
	current_output(0);
}

static void _stop_output(void)
{
	current_bridge_shutdown();
	current_output(0);
}

static void _initial(uint32_t freq_lvl, uint32_t mA_lvl)
{
	ticks_per_step = hbridge_ticks_per_step[freq_lvl];
	current_coefficient = current_trans_intensity(mA_lvl);
	hbridge_step = 0;
	ticks_per_step_temp = ticks_per_step;
	ticks_check_step = 0;
}
const struct current_function current_mode_4_func = {
	.tick_func = _tick_service,
	.change_freq = _set_frequecy,
	.change_elec = _set_elec,
	.start = _start_output,
	.initial = _initial,
	.stop = _stop_output,
};








